import Dexie from "dexie";
export default {
  name: "indexed",
  data() {
    return {
      indexed: {
        db: null
      }
    };
  },
  methods: {
    connectionDatabase(name, table, version) {
      this.indexed.db = new Dexie(name);
      this.indexed.db.version(version).stores(table);
    },
    saveDatabase(table, data) {
      let _self = this;
      this.indexed.db
        .transaction("rw", this.indexed.db[table], async function() {
          await _self.indexed.db[table].add(data);
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    selectDatabase(table, data, callback) {
      let _self = this;
      this.indexed.db
        .transaction("r", this.indexed.db[table], function() {
          _self.indexed.db[table].get(data, function(result) {
            callback(result);
          });
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    clearDatabase(table) {
      let _self = this;
      this.indexed.db
        .transaction("rw", this.indexed.db[table], function() {
          _self.indexed.db[table].clear();
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    alterDatabase(table, key, data, callback) {
      let _self = this;
      this.indexed.db
        .transaction("rw", this.indexed.db[table], function() {
          _self.indexed.db[table].update(key, data, function(result) {
            callback(result);
          });
        })
        .catch(function(error) {
          console.log(error);
        });
    }
  }
};
